﻿/*
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003-2006 Frederico Caldeira Knabben
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.opensource.org/licenses/lgpl-license.php
 * 
 * For further information visit:
 * 		http://www.fckeditor.net/
 * 
 * "Support Open Source software. What about a donation today?"
 * 
 * File Name: fck_flash.js
 * 	Scripts related to the Flash dialog window (see fck_flash.html).
 * 
 * File Authors:
 * 		Frederico Caldeira Knabben (fredck@fckeditor.net)
 */

var oEditor		= window.parent.InnerDialogLoaded() ;
var FCK			= oEditor.FCK ;
var FCKLang		= oEditor.FCKLang ;
var FCKConfig	= oEditor.FCKConfig ;

//#### Dialog Tabs

// Set the dialog tabs.
window.parent.AddTab( 'Info', oEditor.FCKLang.DlgInfoTab ) ;

if ( FCKConfig.VedioUpload )
	window.parent.AddTab( 'Upload', FCKLang.DlgLnkUpload ) ;

if ( !FCKConfig.VedioDlgHideAdvanced )
	window.parent.AddTab( 'Advanced', oEditor.FCKLang.DlgAdvancedTag ) ;

// Function called when a dialog tag is selected.
function OnDialogTabChange( tabCode )
{
	ShowE('divInfo'		, ( tabCode == 'Info' ) ) ;
	ShowE('divUpload'	, ( tabCode == 'Upload' ) ) ;
	ShowE('divAdvanced'	, ( tabCode == 'Advanced' ) ) ;
}

// Get the selected flash embed (if available).
var oFakeImage = FCK.Selection.GetSelectedElement() ;
 
var oEmbed ;

if ( oFakeImage )
{
	if ( oFakeImage.tagName == 'IMG' && oFakeImage.getAttribute('_fckvedio') )
		oEmbed = FCK.GetRealElement( oFakeImage ) ;
	else
		oFakeImage = null ;
}

window.onload = function()
{
	// Translate the dialog box texts.
	oEditor.FCKLanguageManager.TranslatePage(document) ;

	// Load the selected element information (if any).
	LoadSelection() ;

	// Show/Hide the "Browse Server" button.
	GetE('tdBrowse').style.display = FCKConfig.VedioBrowser	? '' : 'none' ;

	// Set the actual uploader URL.
	if ( FCKConfig.VedioUpload )
		GetE('frmUpload').action = FCKConfig.VedioUploadURL ;

	window.parent.SetAutoSize( true ) ;

	// Activate the "OK" button.
	window.parent.SetOkButton( true ) ;
}

function LoadSelection()
{
 
	if ( ! oEmbed ) return ;

	var sUrl = GetAttribute( oEmbed, 'src', '' ) ;

	GetE('txtUrl').value    = GetAttribute( oEmbed, 'src', '' ) ;
	GetE('txtWidth').value  = GetAttribute( oEmbed, 'width', '' ) ;
	GetE('txtHeight').value = GetAttribute( oEmbed, 'height', '' ) ;

	// Get Advances Attributes
	GetE('txtAttId').value		= oEmbed.id ;
	GetE('chkAutoPlay').checked	= GetAttribute( oEmbed, 'play', 'true' ) == 'true' ;
	GetE('chkLoop').checked		= GetAttribute( oEmbed, 'loop', 'true' ) == 'true' ;
	GetE('chkMenu').checked		= GetAttribute( oEmbed, 'menu', 'true' ) == 'true' ;
	GetE('cmbScale').value		= GetAttribute( oEmbed, 'scale', '' ).toLowerCase() ;
	
	GetE('txtAttTitle').value		= oEmbed.title ;

	if ( oEditor.FCKBrowserInfo.IsIE )
	{
		GetE('txtAttClasses').value = oEmbed.getAttribute('className') || '' ;
		GetE('txtAttStyle').value = oEmbed.style.cssText ;
	}
	else
	{
		GetE('txtAttClasses').value = oEmbed.getAttribute('class',2) || '' ;
		GetE('txtAttStyle').value = oEmbed.getAttribute('style',2) ;
	}
 

	UpdatePreview() ;
}

//#### The OK button was hit.
function Ok()
{
	if ( GetE('txtUrl').value.length == 0 )
	{
		window.parent.SetSelectedTab( 'Info' ) ;
		GetE('txtUrl').focus() ;

		alert( oEditor.FCKLang.DlgAlertUrl ) ;

		return false ;
	}
	 
	if ( !oEmbed )
	{
		oEmbed		= FCK.EditorDocument.createElement( '<Object>' );
		oEmbed.id="mPlayer1";
		oEmbed.classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6";
		if(GetE('txtHeight').value > 0 && GetE('txtHeight').value < 200){
			oEmbed.width=GetE('txtWidth').value;
		}else{
			oEmbed.width="200px";	
		}
		if(GetE('txtHeight').value > 0 && GetE('txtHeight').value < 200){
			oEmbed.Height=GetE('txtHeight').value;
		}else{
			oEmbed.Height="200px";	
		}
		var url_ = FCK.EditorDocument.createElement('param');
	 
		url_.name='URL';
		url_.value= GetE('txtUrl').value;
		oEmbed.appendChild(url_);
		oFakeImage  = null ;
	}
	//UpdateEmbed( oEmbed ) ;
	
	if ( !oFakeImage )
	{
		//oFakeImage	= oEditor.FCKDocumentProcessor_CreateFakeImage( 'FCK__Flash', oEmbed ) ;
		//oFakeImage.setAttribute( '_fckflash', 'true', 0 ) ;
		oFakeImage=oEmbed;
		 oFakeImage	= FCK.InsertElementAndGetIt( oFakeImage ) ;
 		
	}
	else{ 
		oEditor.FCKUndo.SaveUndoStep() ;
	}
	oEditor.FCKFlashProcessor.RefreshView( oFakeImage, oEmbed ) ;

	return true ;
}

function UpdateEmbed( e )
{
	 
	SetAttribute( e, 'type'			, 'application/x-shockwave-flash' ) ;
	SetAttribute( e, 'pluginspage'	, 'http://www.macromedia.com/go/getflashplayer' ) ;

	e.src = GetE('txtUrl').value ;
	SetAttribute( e, "width" , GetE('txtWidth').value ) ;
	SetAttribute( e, "height", GetE('txtHeight').value ) ;
	
	// Advances Attributes

	SetAttribute( e, 'id'	, GetE('txtAttId').value ) ;
	SetAttribute( e, 'scale', GetE('cmbScale').value ) ;
	
	SetAttribute( e, 'play', GetE('chkAutoPlay').checked ? 'true' : 'false' ) ;
	SetAttribute( e, 'loop', GetE('chkLoop').checked ? 'true' : 'false' ) ;
	SetAttribute( e, 'menu', GetE('chkMenu').checked ? 'true' : 'false' ) ;

	SetAttribute( e, 'title'	, GetE('txtAttTitle').value ) ;

	if ( oEditor.FCKBrowserInfo.IsIE )
	{
		SetAttribute( e, 'className', GetE('txtAttClasses').value ) ;
		e.style.cssText = GetE('txtAttStyle').value ;
	}
	else
	{
		SetAttribute( e, 'class', GetE('txtAttClasses').value ) ;
		SetAttribute( e, 'style', GetE('txtAttStyle').value ) ;
	}
}

var ePreview ;

function SetPreviewElement( previewEl )
{
	ePreview = previewEl ;
	
	if ( GetE('txtUrl').value.length > 0 )
		UpdatePreview() ;
}

function UpdatePreview()
{
	if ( !ePreview )
		return ;
	while ( ePreview.body.firstChild )
		ePreview.body.removeChild( ePreview.body.firstChild ) ;

	if ( GetE('txtUrl').value.length == 0 )
		ePreview.innerHTML = '&nbsp;' ;
	else
	{ 
	   var txtUrl_t = GetE('txtUrl').value;
	   if(txtUrl_t.substring(txtUrl_t.indexOf("."),txtUrl_t.length).toLowerCase().indexOf("rm") != -1){
	   	     var d_t = ePreview.body.ownerDocument;
	   	     var b_t = d_t.createElement("<input type=button name='hah' value='preview' onclick='test()'>");
	   	    /**
	    		var b_t = d_t.createElement("<OBJECT ID=video1 CLASSID='clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA' HEIGHT=100% WIDTH=100%>");
	    		
				 	var b_t1=d_t.createElement("<param name='_ExtentX' value='9313'>");
				 	b_t.appendChild(b_t1);
				 	var b_t2=d_t.createElement("<param name='_ExtentY' value='7620'>");
				 	b_t.appendChild(b_t2);
				 	var b_t3=d_t.createElement("<param name='AUTOSTART' value='0'>");
				 	b_t.appendChild(b_t3);
				 	var b_t4=d_t.createElement("<param name='SHUFFLE' value='0'>");
				 	b_t.appendChild(b_t4);
				 	var b_t5=d_t.createElement("<param name='PREFETCH' value='0'>");
				 	b_t.appendChild(b_t5);
				 	var b_t6=d_t.createElement("<param name='NOLABELS' value='0'>");
				 	b_t.appendChild(b_t6);
				 	var b_t7=d_t.createElement("<param name='SRC' value='HTTP://192.168.0.1/onlinebusiness/UserFiles/Vedio/1162007553234578.rm'>");
				 	b_t.appendChild(b_t7);
				 	var b_t8=d_t.createElement("<param name='CONTROLS' value='ImageWindow,ControlPanel'>");                  
				 	b_t.appendChild(b_t8);
				 	var b_t9=d_t.createElement("<param name='CONSOLE' value='Clip1'>");                  
				 	b_t.appendChild(b_t9);
				 	var b_t10=d_t.createElement("<param name='NUMLOOP' value='0'>");
				 	b_t.appendChild(b_t10);
				 	var b_t11=d_t.createElement("<param name='CENTER' value='0'>");
				 	b_t.appendChild(b_t11);
				 	var b_t12=d_t.createElement("<param name='MAINTAINASPECT' value='0'>");                  
				 	b_t.appendChild(b_t12);
				 	var b_t13=d_t.createElement("<param name='BACKGROUNDCOLOR' value='#000000'>");                  
				  b_t.appendChild(b_t13);
	   	    
	   	    **/
	   	    ePreview.body.appendChild(b_t);
	   			//ePreview.write("<input type=button name='hah' value='preview' onclick='test()'>");
	   }else{
	   			ePreview.write("<object id='mPlayer1' classid='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6' width='100%' height='100%'><param name='URL' value='"+txtUrl_t+"'></object>");
	   }
	   
	  /**
		var oDoc	=   ePreview.ownerDocument || ePreview.document ;
	
		 
		//var e		= oDoc.createElement( 'EMBED' ) ;
		
		//e.src		= GetE('txtUrl').value ;
		//e.type		= 'application/x-shockwave-flash' ;
		//e.width		= '100%' ;
		//e.height	= '100%' ;
		
	
		var e = oDoc.createElement('object');       
		e.id = "mPlayer1";
		e.width = '50%';
		e.Height = '50%';
		e.classid = 'CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6';
			
		//add Param
		var url_ = oDoc.createElement('param');
	 
		url_.name='URL';
		url_.value= "http://192.168.0.1"+GetE('txtUrl').value;
		e.appendChild(url_);
	
		ePreview.appendChild( e ) ;
				alert("her else end"+oDoc.body.innerHTML);**/
	}
}

// <embed id="ePreview" src="fck_flash/claims.swf" width="100%" height="100%" style="visibility:hidden" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">

function BrowseServer()
{
	OpenFileBrowser( FCKConfig.VedioBrowserURL, FCKConfig.VedioBrowserWindowWidth, FCKConfig.VedioBrowserWindowHeight ) ;
}

function SetUrl( url, width, height )
{
	GetE('txtUrl').value = url ;
	
	if ( width )
		GetE('txtWidth').value = width ;
		
	if ( height ) 
		GetE('txtHeight').value = height ;
 	
 
	UpdatePreview() ;
 
	window.parent.SetSelectedTab( 'Info' ) ;
}

function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
{
	switch ( errorNumber )
	{
		case 0 :	// No errors
			alert( '上传成功!' ) ;
			break ;
		case 1 :	// Custom error
			alert( customMsg ) ;
			return ;
		case 101 :	// Custom warning
			alert( customMsg ) ;
			break ;
		case 201 :
			alert( '该文件已在服务器上存在，现将文件重命名为"' + fileName + '"' ) ;
			break ;
		case 202 :
			alert( '文件类型必须为wav或wmv或wma' ) ;
			return ;
		case 203 :
			alert( "安全异常，请检查权限" ) ;
			return ;
		case 205 :
			alert( '文件大小不能超过2M' ) ;
			return ;
		default :
			alert( '上传失败。代码: ' + errorNumber ) ;
			return ;
	}

	SetUrl( fileUrl ) ;
	GetE('frmUpload').reset() ;
}

var oUploadAllowedExtRegex	= new RegExp( FCKConfig.VedioUploadAllowedExtensions, 'i' ) ;
var oUploadDeniedExtRegex	= new RegExp( FCKConfig.VedioUploadDeniedExtensions, 'i' ) ;

function CheckUpload()
{
	var sFile = GetE('txtUploadFile').value ;
	
	if ( sFile.length == 0 )
	{
		alert( 'Please select a file to upload' ) ;
		return false ;
	}
	
	if ( ( FCKConfig.VedioUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
		( FCKConfig.VedioUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
	{
		OnUploadCompleted( 202 ) ;
		return false ;
	}
	
	return true ;
}